#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>

int main()
{
    pid_t pid = fork();
    if (pid < 0)
    {
        perror("error");
    }
    else if(0 == pid)
    {
        printf("child == [%d]\n", getpid());
        sleep(3);
        int* p = NULL;
        *p = 10;
        exit(-1);
    }
    else
    {
        int status = 0;
        //waitpid(-1, NULL, 0);//相当于wait
        //相当于等待上面的子进程
        //waitpid(pid, NULL, 0);
        //非阻塞等待
        int count = 0;
        while(waitpid(pid, &status, WNOHANG) == 0)
        {
            count++;
        }
        if((status & 0x7f) == 0)
        {
            printf("正常退出：[%d]\n", (status>>8) & 0xff);
        }
        else
        {
            printf("不是正常退出滴sig：[%d]\n", status & 0x7f);
        }
        printf("parent == [%d][%d]:count:[%d]\n", getpid(), status, count);
        while(1)
        {
            ;
        }
    }
    return 0;
}
